package com.jiangjiang.eshop.order.controller;

import com.jiangjiang.eshop.order.biz.OrderInfoBiz;
import com.jiangjiang.eshop.order.dto.CalculateCouponDiscountPriceDTO;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName OrderInfoController
 * @Description TODO
 * @Author jhw
 * @Date 2021/7/22 11:46
 * @Version 1.0
 **/
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderInfoController {
    @Autowired
    private OrderInfoBiz orderInfoBiz;

    /**
     * 计算订单价格
     * @param order 订单数据
     * @return
     */
    @PostMapping("/price")
    public OrderInfoDTO calculateOrderPrice(@RequestBody OrderInfoDTO order) {
        try {
             return orderInfoBiz.calculateOrderPrice(order);
        } catch (Exception e) {
            log.error("error", e);
            return order;
        }
    }

    /**
     * 计算优惠券抵扣的金额
     * @param dto 入参
     * @return
     */
    @PostMapping("/coupon")
    public OrderInfoDTO calculateCouponDiscountPrice( @RequestBody CalculateCouponDiscountPriceDTO dto) {
        try {
           return orderInfoBiz.calculateCouponDiscountPrice(dto.getOrder(),dto.getCoupon());
        } catch (Exception e) {
            log.error("error", e);
            return dto.getOrder();
        }
    }

    /**
     * 订单确认
     * @param orderInfoDTO
     * @return
     */
    @PostMapping("/confirmOrder")
    public OrderInfoDTO confirmOrder(@RequestBody OrderInfoDTO orderInfoDTO){
        try {
           return orderInfoBiz.confirmOrder(orderInfoDTO);
        }catch (Exception e){
            log.error("OrderInfoController confirmOrder error",e);
        }
        return orderInfoDTO;
    }

    /**
     * 取消订单
     * @param id 订单id
     * @return 处理结果
     */
    @GetMapping("/cancel/{id}")
    public Boolean cancel(@PathVariable("id") Long id){
        try {
            return orderInfoBiz.cancel(id);
        }catch (Exception e){
            log.warn("error",e);
        }
        return false;
    }

    /**
     * 订单支付
     * @param id 订单id
     * @return 支付二维码
     */
    @PostMapping("/pay/{id}")
    public String pay(@PathVariable("id")Long id){
        try {
            return orderInfoBiz.pay(id);
        }catch (Exception e){
            log.warn("OrderInfoController pay error",e);
        }
        return null;
    }


    @GetMapping("/updateReturnGoodsLogisticCode")
    public Boolean updateReturnGoodsLogisticCode(@RequestParam("id") Long id,
                                                 @RequestParam("returnGoodsLogisticCode") String returnGoodsLogisticCode){
        try {
            return orderInfoBiz.updateReturnGoodsLogisticCode(id,returnGoodsLogisticCode);
        }catch (Exception e){
            log.warn("OrderInfoController updateReturnGoodsLogisticCode error",e);
        }
        return false;
    }

    /**
     * 确认手动收货
     * @param id 订单id
     * @return 处理结果
     */
    @PostMapping("/confirmReceipt/{id}")
    public Boolean manualConfirmReceipt(@PathVariable("id")Long id){
        try {
            return orderInfoBiz.manualConfirmReceipt(id);
        }catch (Exception e){
             log.warn("OrderInfoController manualConfirmReceipt error",e);
        }
        return false;
    }

}
